package com.hxy.manager.activity.today;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import java.io.File;

import com.hxy.manager.R;

/**
 * Created by 傲寒宏志 on 2018/1/13.
 */

public class TodayShowActivity extends Activity {
    TextView textView7;
    TextView textView8;
    TextView textView9;
    ImageView imageView;
    String mPath;
    String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.today_show);
        Log.i("kan","设置todayshow布局");
        setTitle("内容");

        getActionBar().setDisplayHomeAsUpEnabled(true);//向上返回
        textView7=(TextView)findViewById(R.id.textView7);
        textView8=(TextView)findViewById(R.id.textView8);
        textView9=(TextView)findViewById(R.id.textView9);
        imageView=(ImageView)findViewById(R.id.imageView7);
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        Log.i("kan","Todayshow接收的id值是："+id);
      //  Toast.makeText(Today_show.this,id,Toast.LENGTH_SHORT).show();
        SQLiteDatabase db = openOrCreateDatabase("My_DB.db",MODE_PRIVATE,null);

        Cursor cursor=db.rawQuery("select * from TodayPrice where id = "+id,null);
        Log.i("kan","cursor的长度是："+cursor.getCount());
while (cursor.moveToNext()){
//莫名其妙的bug，把图片的设置语句放在textView7的上面就会导致系统崩溃，放在textView9的下面就正常了
    textView7.setText(cursor.getString(cursor.getColumnIndex("name")));
    textView8.setText(cursor.getString(cursor.getColumnIndex("price")));
    textView9.setText(cursor.getString(cursor.getColumnIndex("date")));
    Log.i("kan","设置setText完成！");
    mPath =cursor.getString(cursor.getColumnIndex("photo")).trim();
    Log.i("kan","图片路径mPath="+mPath);

    Bitmap bmp = BitmapFactory.decodeFile(cursor.getString(cursor.getColumnIndex("photo")));
    cursor.close();
    db.close();
    imageView.setImageBitmap(bmp);
    //单击打开相册
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.i("kan","点击了图片");

            if(!mPath.equals("")){
                Log.i("kan","路径不为空！mPath="+mPath);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setDataAndType(Uri.fromFile(new File(mPath)), ".jpg");
                startActivity(intent);
            }
            else
                Toast.makeText(TodayShowActivity.this,"没有图片",Toast.LENGTH_SHORT).show();

         //   mPath 是图片路劲
           // mType 是图片mimetype，.png对应image/png，.gif对应image/gif，.jpg对应image/jpg，.jpeg对应image/jpeg，.bmp对应image/bmp
        }
    });


}







    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("kan","TodayShow的onResume()的方法启动");//首次启动也会启动这个方法

        textView7 = (TextView) findViewById(R.id.textView7);
        textView8 = (TextView) findViewById(R.id.textView8);
        textView9 = (TextView) findViewById(R.id.textView9);
        imageView = (ImageView) findViewById(R.id.imageView7);
        Log.i("kan","传入的id值："+id);

        SQLiteDatabase db = openOrCreateDatabase("My_DB.db", MODE_PRIVATE, null);
        Cursor cursor = db.rawQuery("select * from TodayPrice where id = " + id, null);
        Log.i("kan","数据库查询成功！cursor的值是"+cursor.getCount());

        if(cursor.getCount()>0){//说明数据是被修改的！
            while (cursor.moveToNext()) {
                mPath = cursor.getString(cursor.getColumnIndex("photo"));
                Bitmap bmp = BitmapFactory.decodeFile(cursor.getString(cursor.getColumnIndex("photo")));
                imageView.setImageBitmap(bmp);
                textView7.setText(cursor.getString(cursor.getColumnIndex("name")));
                textView8.setText(cursor.getString(cursor.getColumnIndex("price")));
                textView9.setText(cursor.getString(cursor.getColumnIndex("date")));

            }
            cursor.close();
            db.close();
        }
        else
            finish();//cursor.getCount()=0说明数据是被删除了,所以直接关闭当前Activity返回Today页面
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){

        getMenuInflater().inflate(R.menu.message,menu);

        return super.onCreateOptionsMenu(menu);
    }

    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
            /*
            case R.id.message://保存
                Toast.makeText(Show.this,"点击了添加！",Toast.LENGTH_SHORT).show();
                break;
                */
        case android.R.id.home:  //默认添加的
		      finish();  
		      break; 
            case R.id.modify:
                Intent intent3 = new Intent(TodayShowActivity.this, TodayUpdateActivity.class);
                intent3.putExtra("id", id);
                startActivity(intent3);
                break;
        }
        return false;

    }
}
